# Poll bot
# by Cee Pee

import sys
import forumapi

def settings(settings):
	settings.add_option("-t", "--thread", type="int", help="thread ID", metavar="ID")
	settings.add_option("-p", "--poll", type="list", help="poll ID", metavar="ID")
	settings.add_option("--results", action="store_true", default=False, help="show results only")

def main(me, options, args):
	poll = me.module.get_attr("Poll").Poll(me, id=options.poll, thread=options.thread)
	
	print("Question: {0}".format(poll.question))
	print("Options:")
	for option in poll.options:
		print("{0}. {1} ({2} voters, {3})".format(option.number, option.name, option.votes, option.percentage))
		if option.voters is not None:
			print(option.voters)
	
	print("Total voters: {0}.".format(poll.total_votes))
	
	if poll.closing_time:
		print("Poll will close at {0}.".format(poll.closing_time))
	
	# Poll votage...
	if not poll.vote_allowed:
		print("You have already voted in this poll.")
	elif not options.results:
		vote_list = input("Option ID(s) and/or name(s) to vote on: ").split(",")
		votes = []
		for vote in vote_list:
			try:
				votes.append(int(vote))
			except:
				votes.append(vote)
		
		poll.vote(votes)
		print("Voted.")

if __name__ == "__main__":
	forumapi.run(main, options=settings, supported_types=["vbulletin"])
